const express = require('express');
const router = express.Router();
const query = require("../util/db.js")
const R = require("../util/resultJson.js")
const md5 = require("md5")

router.get("/",function (req,res) {
    res.render("userinfo/userinfo-list")
})

router.get("/edit",function (req,res) {
    res.render("userinfo/userinfo-edit")
});

router.get("/info",function (req,res) {
    res.json(R.success(req.loginInfo))
})

router.get("/info/:id",function (req,res) {
    let sql = "select * from userinfo where id = ?";
    query(sql,[req.params.id]).then(list=>{
        if(list.length > 0){
            res.json(R.success(list[0]));
        }else {
            res.json(R.error("编号异常！"))
        }
    })
})

router.get("/page",function (req,res) {
    const {page,limit,username,nickname,phone,sex,status,role} = req.query;
    let sql = "select * from userinfo where 1 = 1 ";
    let objs = [];
    if(username){
        sql+='and username = ? ';
        objs.push(username);
    }
    if(nickname){
        sql+="and nickname like ? ";
        objs.push("%"+nickname+"%");
    }
    if(phone){
        sql+="and phone = ? ";
        objs.push(phone);
    }
    if(sex){
        sql+="and sex = ? ";
        objs.push(sex);
    }
    if(status){
        sql+="and status = ? ";
        objs.push(status)
    }
    if(role){
        sql+="and role = ? ";
        objs.push(role);
    }
    query(sql,objs).then(list=>{
        const count = list.length;
        sql+="order by id desc limit ?,?";
        objs.push((page-1)*limit);
        objs.push(parseInt(limit));
        query(sql,objs).then(data=>{
            res.json(R.page(count,data));
        })
    })
})

router.post("/update",function (req,res) {
    const userinfo = req.body;
    if(userinfo.id){
        let sql = "update userinfo set nickname = ?,phone = ?,sex = ?,role = ?,avatar = ? where id = ?";
        query(sql,[userinfo.nickname,userinfo.phone,userinfo.sex,userinfo.role,userinfo.avatar,userinfo.id]).then(r=>{
            res.json(R.success());
        })
    }else{
        let sql = "select * from userinfo where username = ?";
        query(sql,[userinfo.username]).then(list=>{
            if(list.length>0){
                res.json(R.error("用户名已存在！"));
            }else{
                sql = "insert into userinfo values(null,?,?,?,?,?,?,?,?)";
                query(sql,[userinfo.username,md5("66668888"),userinfo.nickname,userinfo.phone,userinfo.sex,1,userinfo.role,userinfo.avatar]).then(r=>{
                    res.json(R.success());
                })
            }
        })
    }
})

router.delete("/delete/:id",function (req,res) {
    if(req.params.id == 1){
        res.json(R.error("超级管理员禁止删除！"))
    }else if(req.params.id == req.loginInfo.id){
        res.json(R.error("当前登录用户禁止删除！"))
    }else{
        let sql = "delete from userinfo where id = ?";
        query(sql,[req.params.id]).then(r=>{
            res.json(R.success())
        })
    }
})

router.post("/status",function (req,res) {
    if(req.body.id == 1){
        res.json(R.error("超级管理员无法更改状态！"))
    }else if(req.body.id == req.loginInfo.id){
        res.json(R.error("当前登录用户无法更改状态！"))
    }else{
        let sql = "update userinfo set status = ? where id = ?";
        query(sql,[req.body.status,req.body.id]).then(r=>{
            res.json(R.success())
        })
    }
})

router.get("/password",function (req,res) {
    res.render("userinfo/userinfo-password")
})

router.post("/password",function (req,res) {
    const {oldPwd,password} = req.body;
    const id = req.loginInfo.id;
    let sql = "select * from userinfo where id = ? and password = ?";
    query(sql,[id,md5(oldPwd)]).then(list=>{
        if(list.length>0){
            sql = "update userinfo set password = ? where id = ?";
            query(sql,[md5(password),id]).then(r=>{
                res.cookie("token","token",{expires:new Date(-1)})
                res.json(R.success());
            })
        }else{
            res.json(R.error("原密码不正确！"))
        }
    })
})

router.post("/reset/password",function (req,res) {
    const {id,password} = req.body;
    let sql = "update userinfo set password = ? where id = ?";
    query(sql,[md5(password),id]).then(r=>{
        res.json(R.success());
    })
})

router.get("/select/:role",function (req,res) {
    query("select * from userinfo where role = ?",[req.params.role]).then(list=>{
        res.json(R.success(list))
    })
})


module.exports = router;
